from django.shortcuts import render
from help_api.models import HelpCard
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json


# Create your views here.
@csrf_exempt
def add_help_card(request):
    print("加载！")
    print(json.loads(request.body))
    if request.method == 'POST':  # 如果等于post请求
        req = json.loads(request.body)
        key_flag = req.get('title') and req.get('signal') and len(req) == 5
        if key_flag:
            print(req)
            title = req['title']
            info = req['info']
            contact = req['contact']
            signal = req['signal']
            province = req['province']
            add_help = HelpCard(title=title, info=info, contact=contact, signal=signal, province=province)
            add_help.save()
            return JsonResponse({"status": "Bs.200", "msg": "sucess!"})
        else:
            return JsonResponse({"status": "Bs.400", "msg": "please check param!"})